package com.cssw.fyzb.core.dao.system;


import com.cssw.fyzb.core.model.dodb.SysOper;
import com.cssw.fyzb.core.model.dodb.SysOperRole;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: YangYiQun
 * @Date: 2020/10/14 14:42
 */
@Repository
public class OperDaoImpl implements OperDao {

    /**
     * 新增账号
     *
     * @param operCode
     * @param operPwd
     * @param mobileNo
     * @param operName
     * @param status
     * @return
     */
    public Long save(String operCode, String operPwd, String mobileNo,
                     String operName, String status,String remark, Long operId) {
        SysOper oper = new SysOper();
        oper.setOperCode(operCode);
        oper.setOperPwd(operPwd);
        oper.setMobileNo(mobileNo);
        oper.setOperName(operName);
        oper.setStatus(status);
        oper.setRemark(remark);
        oper.setCreateOperId(operId);
        oper.setCreateTime(new Date());
        oper.save();
        return oper.getOperId();
    }

    /**
     * 修改账号
     *
     * @param operId
     * @param operCode
     * @param mobileNo
     * @param operName
     * @param status
     */
    public void update(Long operId, String operCode, String mobileNo,
                       String operName, String status, String remark, Long sysOperId) {
        SysOper oper = new SysOper();
        oper.setOperId(operId);
        oper.setOperCode(operCode);
        oper.setMobileNo(mobileNo);
        oper.setOperName(operName);
        oper.setStatus(status);
        oper.setRemark(remark);
        oper.setUpdateOperId(sysOperId);
        oper.setUpdateTime(new Date());
        oper.update();
    }

    /**
     * 修改账号状态
     *
     * @param operId
     * @param status
     */
    public void updateStatus(Long operId, String status, Long sysOperId) {
        SysOper oper = new SysOper();
        oper.setOperId(operId);
        oper.setStatus(status);
        oper.setUpdateOperId(sysOperId);
        oper.setUpdateTime(new Date());
        oper.update();
    }

    /**
     * 根据账号编码查询账号信息
     *
     * @param operCode
     * @return
     */
    public SysOper getByCode(String operCode) {
        String sql = "select * from sys_oper where oper_code = ? and state='1' ";
        return SysOper.dao.findFirst(sql, operCode);
    }

    /**
     * 根据账号标识查询账号信息
     *
     * @param operId
     * @return
     */
    public SysOper getById(Long operId) {
        return SysOper.dao.findById(operId);
    }

    /**
     * 更新账号密码
     *
     * @param operId
     * @param operPwd
     */
    public void updateOperPwd(Long operId, String operPwd) {
        SysOper oper = new SysOper();
        oper.setOperId(operId);
        oper.setOperPwd(operPwd);
        oper.update();
    }

    /**
     * 更新身份证号码
     *
     * @param operId
     * @param idCard
     */
    public void updateIdCard(Long operId, String idCard) {
        SysOper oper = new SysOper();
        oper.setOperId(operId);
        oper.update();
    }

    /**
     * 删除账号信息
     *
     * @param operId
     */
    public void delete(Long operId, Long sysOperId) {
        SysOper oper = new SysOper();
        oper.setOperId(operId);
        oper.setState(SysOper.STATE_DELETE);
        oper.setUpdateOperId(sysOperId);
        oper.setUpdateTime(new Date());
        oper.update();
    }

    /**
     * 查询账号列表
     *
     * @param param
     * @param page
     * @param limit
     * @return
     */
    public Page<SysOper> list(Map param, Integer page, Integer limit) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" select t1.*, ");
        sqlParse.addSQL(" (select oper_name from sys_oper t2 where t2.oper_id = t1.create_oper_id) as create_oper_name  ");
        sqlParse.addSQL(" from sys_oper t1 ");
        sqlParse.addSQL(" where state='1' ");
        sqlParse.addSQL(" and oper_code = :operCode ");
        sqlParse.addLikeSQL(" and oper_name like :operName ");
        sqlParse.addLikeSQL(" and mobile_no like :mobileNo ");
        sqlParse.addLikeSQL(" and id_card like :idCard ");
        sqlParse.addSQL(" order by oper_id desc ");

        return SysOper.dao.paginate(page, limit, sqlParse);
    }

    @Override
    public List<SysOperRole> queryOperRole(Long operId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select t1.*, ")
                .append(" (select t2.role_name from sys_role t2 where t2.role_id = t1.role_id) as role_name ")
                .append(" from sys_oper_role t1 where t1.oper_id = ?");
        return SysOperRole.dao.find(sql.toString(), operId);
    }

    @Override
    public void deleteOperRoleByOperId(Long operId) {
        Db.delete("delete from sys_oper_role where oper_id = ?", operId);
    }

    @Override
    public void batchSaveOperRole(List<SysOperRole> operRoles) {
        Db.batchSave(operRoles, 100);
    }


}
